//
//  GradientBackground.swift
//  Kosspot
//
//  Created by 李鑫 on 2025/6/19.
//

import SwiftUI
public struct GradientBackgroundModifier: ViewModifier {
    let colors: [Color]
    let type: GradientType
    let cornerRadius: CGFloat
    
    public enum GradientType {
        case linear(start: UnitPoint, end: UnitPoint)
        case radial(center: UnitPoint, startRadius: CGFloat, endRadius: CGFloat)
    }
    
    public func body(content: Content) -> some View {
        content
            .background(
                Group {
                    switch type {
                    case .linear(let start, let end):
                        LinearGradient(
                            gradient: Gradient(colors: colors),
                            startPoint: start,
                            endPoint: end
                        )
                    case .radial(let center, let startRadius, let endRadius):
                        RadialGradient(
                            gradient: Gradient(colors: colors),
                            center: center,
                            startRadius: startRadius,
                            endRadius: endRadius
                        )
                    }
                }
                .cornerRadius(cornerRadius)
            )
    }
}

public extension View {
    func gradientBackground(
        colors: [Color],
        type: GradientBackgroundModifier.GradientType = .linear(start: .top, end: .bottom),
        cornerRadius: CGFloat = 0
    ) -> some View {
        self.modifier(
            GradientBackgroundModifier(
                colors: colors,
                type: type,
                cornerRadius: cornerRadius
            )
        )
    }
}
